package com.orange.config;

import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

import javax.annotation.Resource;
import java.util.ArrayList;

/**
 * @author Emp
 * @projectName: springboot
 * @package: com.orange.config
 * @className: SwaggerAutoConfiguration
 * @description: TODO
 * @date: 2021/12/7 10:00
 * @version: 1.0
 */
@Configuration
@EnableConfigurationProperties(SwaggerProperties.class)
public class SwaggerAutoConfiguration {

    @Resource
    private SwaggerProperties swaggerProperties;

    @Bean
    public Docket docket(){
        return new Docket(DocumentationType.OAS_30).apiInfo(getApiInfo()).groupName(swaggerProperties.getGroupName()).select()
                .apis(RequestHandlerSelectors.withClassAnnotation(RestController.class)).build();
    }

    private ApiInfo getApiInfo(){
        Contact contact = new Contact(swaggerProperties.getName(), swaggerProperties.getUrl(), swaggerProperties.getEmail());
        return new ApiInfo(swaggerProperties.getTitle(), swaggerProperties.getDescription(), swaggerProperties.getVersion()
                , swaggerProperties.getTermsOfServiceUrl(), contact, swaggerProperties.getLicense(), swaggerProperties.getLicenseUrl(),new ArrayList<>());
    }
}
